package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DiscountFactor;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroupAudit;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.DiscountService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;

/**
 * 发布整车Controller
 * 
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tRentgroup")
public class TRentgroupController extends BaseController {

	@Autowired
	private TRentgroupService tRentgroupService;
	@Autowired
	private TRentproductService tRentproductService;
	@Autowired
	private TCargotypeService tCargotypeService;
	@Autowired
	private TSitedtlService tSitedtlService;
	@Autowired
	private TLineService tLineService;
	@Autowired
	private DiscountService  DiscountService;

	@ModelAttribute
	public TRentgroup get(@RequestParam(required = false) String rentgrpid) {
		TRentgroup entity = null;
		if (StringUtils.isNotBlank(rentgrpid)) {
			entity = tRentgroupService.get(rentgrpid);
			TRentproduct qry = new TRentproduct();
			qry.setRentgrpid(entity.getRentgrpid());
			entity.setProductList(tRentproductService.findList(qry));
		}
		if (entity == null) {
			entity = new TRentgroup();
		}
		return entity;
	}

	@RequiresPermissions("rentproduct:tRentgroup:allview")
	@RequestMapping(value = { "list", "" })
	public String list(TRentgroup tRentgroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRentgroup> page = tRentgroupService.findAll(new Page<TRentgroup>(request, response), tRentgroup);
		model.addAttribute("page", page);
		model.addAttribute("tRentgroup", tRentgroup);//1005
		
		/*TLine line = new TLine();
		line.setLinestatus(TLine.LINESTATUS_VALID);
		List<TLine> lineList = tLineService.findList(line);//2814
		model.addAttribute("lineList", lineList);*/
		
		return "modules/rentproduct/tRentgroupList";
	}
	
	@RequiresPermissions("rentproduct:tRentgroup:allview")
	@RequestMapping(value = "form")
	public String form(TRentgroup tRentgroup, Model model) {
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("tRentgroup", tRentgroup);
		return "modules/rentproduct/tRentgroupForm";
	}




	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "planeList")
	public String planeList(TRentgroup tRentgroup, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_AIRPORT);
		Page<TRentgroup> page = tRentgroupService.findPage(new Page<TRentgroup>(request, response), tRentgroup);
		model.addAttribute("page", page);
		model.addAttribute("tRentgroup", tRentgroup);
		TLine line = new TLine();
		line.setLinetype(TLine.LINE_TYPE_PLANE);
		line.setLinestatus(TLine.LINESTATUS_VALID);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("lineList", lineList);
		return "modules/rentproduct/rentGroupForPlaneList";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "trainList")
	public String trainList(TRentgroup tRentgroup, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_TRAIN);
		Page<TRentgroup> page = tRentgroupService.findPage(new Page<TRentgroup>(request, response), tRentgroup);
		model.addAttribute("page", page);
		model.addAttribute("tRentgroup", tRentgroup);
		TLine line = new TLine();
		line.setLinetype(TLine.LINE_TYPE_TRAIN);
		line.setLinestatus(TLine.LINESTATUS_VALID);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("lineList", lineList);
		return "modules/rentproduct/rentGroupForTrainList";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "dayList")
	public String dayList(TRentgroup tRentgroup, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		// 读取所有按天包车
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_DAY);
		Page<TRentgroup> page = tRentgroupService.findPage(new Page<TRentgroup>(request, response), tRentgroup);

		model.addAttribute("page", page);
		model.addAttribute("tRentgroup", tRentgroup);

		// 读取站点
		TSitedtl site = new TSitedtl();
		model.addAttribute("siteList", tSitedtlService.findList(site));

		return "modules/rentproduct/rentGroupForDayList";
	}

	@RequiresPermissions("rentproduct:tRentgroupAudit:view")
	@RequestMapping(value = "rentforaudit")
	public String rentforaudit(TRentgroup tRentgroup, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tRentgroup.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
		Page<TRentgroup> page = tRentgroupService.findPage(new Page<TRentgroup>(request, response), tRentgroup);
		model.addAttribute("page", page);
		model.addAttribute("tRentgroup", tRentgroup);
		TLine line = new TLine();
		line.setLinestatus(TLine.LINESTATUS_VALID);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("lineList", lineList);
		return "modules/rentproduct/rentGroupForAuditList";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "travelList")
	public String travelList(TRentgroup tRentgroup, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_TRAVEL);
		Page<TRentgroup> page = tRentgroupService.findPage(new Page<TRentgroup>(request, response), tRentgroup);
		model.addAttribute("page", page);
		model.addAttribute("tRentgroup", tRentgroup);
		TLine line = new TLine();
		line.setLinestatus(TLine.LINESTATUS_VALID);
		line.setLinetype(TLine.LINE_TYPE_TRAVEL);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("lineList", lineList);
		return "modules/rentproduct/rentGroupForTravelList";
	}
	
	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "travelForm")
	public String travelForm(TRentgroup tRentgroup, Model model) {
		if (ValidateTool.isEmptyOrNull(tRentgroup.getBrand())) {
			tRentgroup.setBrand("金龙宇通同等级");
		}
		TLine line = new TLine();
		TSitedtl site = new TSitedtl();

		Office office = UserUtils.getUser().getOffice();
		site.setCitycode(office.getArea().getCode());
		line.setDepartcity(site);
		line.setLinestatus(TLine.LINESTATUS_VALID);
		line.setLinetype(TLine.LINE_TYPE_TRAVEL);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("lineList", lineList);
		model.addAttribute("tRentgroup", tRentgroup);

		return "modules/rentproduct/rentGroupForTravelForm";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "travelFormView")
	public String travelFormView(TRentgroup tRentgroup, String viewtype, Model model) {
		TLine line = new TLine();

		line.setLinestatus(TLine.LINESTATUS_VALID);
		line.setLinetype(TLine.LINE_TYPE_TRAVEL);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("lineList", lineList);
		model.addAttribute("tRentgroup", tRentgroup);

		// viewtype,如果是审核处调用为audit,如果是维护人员调用为deal
		model.addAttribute("viewtype", viewtype);

		return "modules/rentproduct/rentGroupForTravelView";
	}
	
	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "planeForm")
	public String planeForm(TRentgroup tRentgroup, Model model) {
		if (ValidateTool.isEmptyOrNull(tRentgroup.getBrand())) {
			tRentgroup.setBrand("金龙宇通同等级");
		}
		TLine line = new TLine();
		TSitedtl site = new TSitedtl();

		Office office = UserUtils.getUser().getOffice();
		site.setCitycode(office.getArea().getCode());
		line.setDepartcity(site);
		line.setLinestatus(TLine.LINESTATUS_VALID);
		line.setLinetype(TLine.LINE_TYPE_PLANE);
		
		List<TLine> lineList = tLineService.findCanList(line);
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("lineList", lineList);
		model.addAttribute("tRentgroup", tRentgroup);

		return "modules/rentproduct/rentGroupForPlaneForm";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "trainForm")
	public String trainForm(TRentgroup tRentgroup, Model model) {
		if (ValidateTool.isEmptyOrNull(tRentgroup.getBrand())) {
			tRentgroup.setBrand("金龙宇通同等级");
		}
		TLine line = new TLine();
		TSitedtl site = new TSitedtl();

		Office office = UserUtils.getUser().getOffice();
		site.setCitycode(office.getArea().getCode());
		line.setDepartcity(site);
		line.setLinestatus(TLine.LINESTATUS_VALID);
		line.setLinetype(TLine.LINE_TYPE_TRAIN);
		List<TLine> lineList = tLineService.findCanList(line);
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("lineList", lineList);
		model.addAttribute("tRentgroup", tRentgroup);

		return "modules/rentproduct/rentGroupForTrainForm";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "dayForm")
	public String dayForm(TRentgroup tRentgroup, Model model) {
		if (ValidateTool.isEmptyOrNull(tRentgroup.getBrand())) {
			tRentgroup.setBrand("金龙宇通同等级");
		}
		// 读取站点
		TSitedtl site = new TSitedtl();
		Office office = UserUtils.getUser().getOffice();
		site.setCitycode(office.getArea().getCode());
		site.setSpottypes(new String[]{TSitedtl.SPOTTYPE_CITY,TSitedtl.SPOTTYPE_SUBURBS});
		model.addAttribute("siteList", tSitedtlService.findList(site));

		model.addAttribute("productnum", tRentgroup.getProductList().size());

		model.addAttribute("tRentgroup", tRentgroup);

		return "modules/rentproduct/rentGroupForDayForm";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "dayFormView")
	public String dayFormView(TRentgroup tRentgroup, String viewtype, Model model) {
		// 读取站点
		TSitedtl site = new TSitedtl();
		model.addAttribute("siteList", tSitedtlService.findList(site));
		model.addAttribute("productnum", tRentgroup.getProductList().size());

		model.addAttribute("tRentgroup", tRentgroup);

		// viewtype,如果是审核处调用为audit,如果是维护人员调用为deal
		model.addAttribute("viewtype", viewtype);

		return "modules/rentproduct/rentGroupForDayView";
	}


	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "planeFormView")
	public String planeFormView(TRentgroup tRentgroup, String viewtype, Model model) {
		TLine line = new TLine();
		line.setLinestatus(TLine.LINESTATUS_VALID);
		line.setLinetype(TLine.LINE_TYPE_PLANE);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("lineList", lineList);
		model.addAttribute("tRentgroup", tRentgroup);

		// viewtype,如果是审核处调用为audit,如果是维护人员调用为deal
		model.addAttribute("viewtype", viewtype);

		return "modules/rentproduct/rentGroupForPlaneView";
	}

	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "trainFormView")
	public String trainFormView(TRentgroup tRentgroup, String viewtype, Model model) {
		TLine line = new TLine();
		line.setLinestatus(TLine.LINESTATUS_VALID);
		List<TLine> lineList = tLineService.findList(line);
		model.addAttribute("productnum", tRentgroup.getProductList().size());
		model.addAttribute("lineList", lineList);
		model.addAttribute("tRentgroup", tRentgroup);

		// viewtype,如果是审核处调用为audit,如果是维护人员调用为deal
		model.addAttribute("viewtype", viewtype);

		return "modules/rentproduct/rentGroupForTrainView";
	}

	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "save")
	public String save(TRentgroup tRentgroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentgroup)) {
			return form(tRentgroup, model);
		}
		if (tRentgroup.getAuditstatus().equals(TRentgroupAudit.AUDITSTATUS_NOT_PASS)) {
			tRentgroup.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
		}
		tRentgroup.setOffice(UserUtils.getUser().getOffice());

		// 判断该包车车型是否已经存在
		List<TRentproduct> tRentproductList = tRentgroup.getProductList();
		if (tRentproductList != null && !tRentproductList.isEmpty()) {
			for (TRentproduct tmp : tRentgroup.getProductList()) {			
				if (tRentgroup.gettLine() != null) {
					// 定义了线路的包车
					if (tRentgroup.getRentgrpid() != null)
						tmp.setRentgrpid(tRentgroup.getRentgrpid());
					tmp.setLine(tRentgroup.gettLine());
					tmp.setOffice(tRentgroup.getOffice());

					List<TRentproduct> tmplist = tRentproductService.findProductList(tmp);
					if (tmplist.size() > 0) {
						addMessage(redirectAttributes, "已经有该车型的同类产品！");
						return form(tRentgroup, model);
					}
				} else {
					// 按天包车
					if (tRentgroup.getRentgrpid() != null)
						tmp.setRentgrpid(tRentgroup.getRentgrpid());
					tmp.setOffice(tRentgroup.getOffice());
					tmp.setTabType(tRentgroup.getRange());
					tmp.setDepartcity(tRentgroup.getDepartcity());
					
					List<TRentproduct> tmplist = tRentproductService.findProductList(tmp);
					if (tmplist.size() > 0) {
						addMessage(redirectAttributes, "已经有该车型的同类产品！");
						return form(tRentgroup, model);
					}
				}
			}
		}
		tRentgroupService.save(tRentgroup);
		addMessage(redirectAttributes, "保存发布整车成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/rentGroupForTravelList?repage";
	}

	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "travelSave")
	public String travelSave(TRentgroup tRentgroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentgroup)) {
			return travelForm(tRentgroup, model);
		}

		if (tRentgroup.getAuditstatus() != null
				&& tRentgroup.getAuditstatus().equals(TRentgroupAudit.AUDITSTATUS_NOT_PASS)) {
			tRentgroup.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
		}
		// 保存为当前用户的客运公司
		tRentgroup.setOffice(UserUtils.getUser().getOffice());
		// 旅游包车
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_TRAVEL);

		// 判断该包车车型是否已经存在
		List<TRentproduct> tRentproductList = tRentgroup.getProductList();
		if (tRentproductList != null && !tRentproductList.isEmpty()) {
			for (TRentproduct tmp : tRentgroup.getProductList()) {
				if (tRentgroup.gettLine() != null) {
					if (tRentgroup.getRentgrpid() != null)
						tmp.setRentgrpid(tRentgroup.getRentgrpid());
					
					tmp.setLine(tRentgroup.gettLine());
					tmp.setOffice(tRentgroup.getOffice());
					List<TRentproduct> tmplist = tRentproductService.findProductList(tmp);
					if (tmplist.size() > 0) {
						addMessage(model, "已经有该车型的同类产品！");
						return travelForm(tRentgroup, model);
					}
				}
			}
		}
		tRentgroupService.save(tRentgroup);
		addMessage(redirectAttributes, "保存发布整车成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/travelList?repage";
	}

	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "planeSave")
	public String planeSave(TRentgroup tRentgroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentgroup)) {
			return planeForm(tRentgroup, model);
		}

		if (tRentgroup.getAuditstatus() != null
				&& tRentgroup.getAuditstatus().equals(TRentgroupAudit.AUDITSTATUS_NOT_PASS)) {
			tRentgroup.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
		}
		// 保存为当前用户的客运公司
		tRentgroup.setOffice(UserUtils.getUser().getOffice());
		// 旅游包车
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_AIRPORT);

		// 判断该包车车型是否已经存在
		List<TRentproduct> tRentproductList = tRentgroup.getProductList();
		if (tRentproductList != null && !tRentproductList.isEmpty()) {
			for (TRentproduct tmp : tRentgroup.getProductList()) {
				if (tRentgroup.gettLine() != null) {
					if (tRentgroup.getRentgrpid() != null)
						tmp.setRentgrpid(tRentgroup.getRentgrpid());
					tmp.setLine(tRentgroup.gettLine());
					tmp.setOffice(tRentgroup.getOffice());
					List<TRentproduct> tmplist = tRentproductService.findProductList(tmp);
					if (tmplist.size() > 0) {
						addMessage(model, "已经有该车型的同类产品！");
						return planeForm(tRentgroup, model);
					}
				}
			}
		}
		tRentgroupService.save(tRentgroup);
		addMessage(redirectAttributes, "保存发布整车成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/planeList?repage";
	}

	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "trainSave")
	public String trainSave(TRentgroup tRentgroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentgroup)) {
			return trainForm(tRentgroup, model);
		}

		if (tRentgroup.getAuditstatus() != null
				&& tRentgroup.getAuditstatus().equals(TRentgroupAudit.AUDITSTATUS_NOT_PASS)) {
			tRentgroup.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
		}
		// 保存为当前用户的客运公司
		tRentgroup.setOffice(UserUtils.getUser().getOffice());
		// 旅游包车
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_TRAIN);

		// 判断该包车车型是否已经存在
		List<TRentproduct> tRentproductList = tRentgroup.getProductList();
		if (tRentproductList != null && !tRentproductList.isEmpty()) {
			for (TRentproduct tmp : tRentgroup.getProductList()) {
				if (tRentgroup.gettLine() != null) {
					if (tRentgroup.getRentgrpid() != null)
						tmp.setRentgrpid(tRentgroup.getRentgrpid());
					tmp.setLine(tRentgroup.gettLine());
					tmp.setOffice(tRentgroup.getOffice());
					List<TRentproduct> tmplist = tRentproductService.findProductList(tmp);
					if (tmplist.size() > 0) {
						addMessage(model, "已经有该车型的同类产品！");
						return trainForm(tRentgroup, model);
					}
				}
			}
		}

		tRentgroupService.save(tRentgroup);
		addMessage(redirectAttributes, "保存发布整车成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/trainList?repage";
	}

	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "daySave")
	public String daySave(TRentgroup tRentgroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentgroup)) {
			return dayForm(tRentgroup, model);
		}

		if (tRentgroup.getAuditstatus() != null
				&& tRentgroup.getAuditstatus().equals(TRentgroupAudit.AUDITSTATUS_NOT_PASS)) {
			tRentgroup.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
		}
		// 保存为当前用户的客运公司
		tRentgroup.setOffice(UserUtils.getUser().getOffice());
		// 旅游包车
		tRentgroup.setRenttype(TRentgroup.RENT_TYPE_DAY);

		// 判断该包车车型是否已经存在
		List<TRentproduct> tRentproductList = tRentgroup.getProductList();
		if (tRentproductList != null && !tRentproductList.isEmpty()) {
			for (TRentproduct tmp : tRentgroup.getProductList()) {
				// 按天包车
				if (tRentgroup.getRentgrpid() != null)
					tmp.setRentgrpid(tRentgroup.getRentgrpid());
				tmp.setOffice(tRentgroup.getOffice());
				tmp.setTabType(tRentgroup.getRange());
				tmp.setDepartcity(tRentgroup.getDepartcity());
				
				List<TRentproduct> tmplist = tRentproductService.findProductList(tmp);
				if (tmplist.size() > 0) {
					addMessage(model, "已经有该车型的同类产品！");
					return dayForm(tRentgroup, model);
				}
			}
		}

		tRentgroupService.save(tRentgroup);
		addMessage(redirectAttributes, "保存发布整车成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/dayList?repage";
	}

	@RequiresPermissions("rentproduct:tRentgroup:delete")
	@RequestMapping(value = "delete")
	public String delete(TRentgroup tRentgroup, RedirectAttributes redirectAttributes) {
		tRentgroupService.delete(tRentgroup);
		addMessage(redirectAttributes, "删除发布整车成功");
		String returnurl = new String();
		if (tRentgroup.getRenttype().equals(TRentgroup.RENT_TYPE_AIRPORT))
			returnurl = "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/planeList?repage";
		else if (tRentgroup.getRenttype().equals(TRentgroup.RENT_TYPE_DAY))
			returnurl = "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/dayList?repage";
		else if (tRentgroup.getRenttype().equals(TRentgroup.RENT_TYPE_TRAVEL))
			returnurl = "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/travelList?repage";
		else if (tRentgroup.getRenttype().equals(TRentgroup.RENT_TYPE_TRAIN))
			returnurl = "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/trainList?repage";

		return returnurl;
	}

	// 查询所有车型
	@RequestMapping(value = "getallcartype")
	@ResponseBody
	public List<TCargotype> getallcartype(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<TCargotype> cargotypelist = tCargotypeService.findalltypes();

		return cargotypelist;
	}

	// 通过车牌号 查询汽车信息
	@RequestMapping(value = "ajaxList")
	@ResponseBody
	public List<TCargotype> ajaxList(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 通过officeid 查询客运公司巴士信息 AJAX
		String cargotypeid = request.getParameter("cargotypeid");
		TCargotype retType = tCargotypeService.get(cargotypeid);
		List<TCargotype> retTypelist = new ArrayList<TCargotype>();
		retTypelist.add(retType);
		return retTypelist;
	}
	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "discountlist")
	public String discountList(DiscountFactor discountfactor, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Office office = UserUtils.getUser().getOffice();
		discountfactor.setOfficename(office.getName());
		List<DiscountFactor> list = DiscountService.getList(discountfactor);
		model.addAttribute("list",list);
		return "modules/rentproduct/tDiscountfactorList";
	}
	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "discountedit")
	public String discountEdit(DiscountFactor discountfactor, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Integer discountid = Integer.valueOf(request.getParameter("discountid"));	
		discountfactor.setDiscountid(discountid);
		discountfactor = DiscountService.getDiscountByLevel(discountfactor);
		model.addAttribute("tDiscountFactor",discountfactor);
		return "modules/rentproduct/tDiscountfactorEdit";
	}
	@RequiresPermissions("rentproduct:tRentgroup:view")
	@RequestMapping(value = "discountform")
	public String discountForm(DiscountFactor discountfactor, HttpServletRequest request,Model model) {
		String discountid = request.getParameter("discountid");
		Office office = UserUtils.getUser().getOffice();
		discountfactor.setOfficename(office.getName());
		discountfactor.setOfficeid(office.getId());
		model.addAttribute("tDiscountFactor", discountfactor);
		return "modules/rentproduct/tDiscountfactorForm";
	}
	@RequiresPermissions("rentproduct:tRentgroup:edit")
	@RequestMapping(value = "discountsave")
	public String discountSave(DiscountFactor discountfactor, HttpServletRequest request,Model model, RedirectAttributes redirectAttributes) {
		if(discountfactor.getDiscountid() != null){
			DiscountService.update(discountfactor);
		}else{
		List<DiscountFactor> discountList =discountfactor.getDiscountList();
		int index = 0;
		if (discountList != null && !discountList.isEmpty()) {
			for( DiscountFactor list : discountfactor.getDiscountList()) {
				   discountfactor.setFactor(Double.parseDouble(list.getId().toString()));
				   index = index + 1;
				   discountfactor.setDistancelevel(String.valueOf(index));
				   DiscountService.save(discountfactor);
			}
		}
		addMessage(redirectAttributes, "保存接送机系数成功");
		}
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentgroup/discountlist?repage";
	}
}